<?php
/**
 * Created by PhpStorm.
 * Script Name: Onmessage.php
 * Create: 12/25/21 9:43 PM
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\admin\controller;


use app\admin\model\Bot;
use app\admin\model\BotMember;
use app\common\controller\BaseCtl;
use ky\Bot\Kam;
use ky\Bot\Qyk;
use ky\Bot\Vlw;
use ky\Bot\Wx;
use ky\Bot\Xp;
use ky\Helper;
use ky\Jtt;
use ky\Logger;

class Onmessage extends BaseCtl
{

    /**
     * @var Bot
     */
    private $botM;
    /**
     * @var BotMember
     */
    private $memberM;
    private $sender;
    private $bot;
    /**
     * @var Wx
     */
    private $botClient;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->botM = new Bot();
        $this->memberM = new BotMember();
    }

    /**
     * vlw框架
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function vlw(){
        if(request()->isPost()) {
            Helper::$ajax = $this->getAjax();
            $this->botClient = $this->getBotClient('vlw', ['base_uri' => '124.222.4.168:8090']);
            $fdj = "wxid_xokb2ezu1p6t21";
            $yyp = 'weiwei562608';
            $jane = "wxid_a98qqf9m4bny22";

            $data = [
                "token" => '123456',//true时，http-sdk才处理，false直接丢弃
                "api" => Vlw::SEND_TEXT_MSG,
                "robot_wxid" => $fdj,//用哪个机器人发
                "to_wxid" => $jane,//发到哪里？群/好友
                "msg" => "test"
            ];

            //Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
            if(empty(Helper::$ajax['content']['from_wxid']) && empty(Helper::$ajax['content']['from_group'])){ //说明机器人本人发
                $wxid = Helper::$ajax['content']['robot_wxid'];
            }else{
                $wxid = Helper::$ajax['content']['from_wxid'];
            }

            if($wxid == $fdj){
                //Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
                $res = $this->botClient->sendTextToFriend(['data' => $data]);
                Logger::error(json_encode($res, JSON_UNESCAPED_UNICODE));
            }
        }else{
            exit('非法请求');
        }
    }

    public function keaimao(){
        if(request()->isPost()) {
            Helper::$ajax = $this->getAjax();
            $this->botClient = $this->getBotClient('kam', ['base_uri' => '192.168.0.108:8090']);
            $fdj = "wxid_xokb2ezu1p6t21";
            $yyp = 'weiwei562608';
            $weixin = 'weixin';
            $jane = "wxid_a98qqf9m4bny22";

            $data = [
                "success" => true,//true时，http-sdk才处理，false直接丢弃
                "message" => "successful!",
                "event" => "SendImageMsg",//告诉它干什么，SendImageMsg是发送图片事件
                "robot_wxid" => $fdj,//用哪个机器人发
                "to_wxid" => $yyp,//发到哪里？群/好友
                "member_wxid" => "",
                "member_name" => "",
                "group_wxid" => "",
                "msg" => ['url' => 'https://b3logfile.com/bing/20201024.jpg', 'name' => '20201024.jpg']
            ];

            Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));

            /*$res = $this->botClient->sendTextToFriend([
                'data' => $data
            ]);

            Logger::info(json_encode($res, JSON_UNESCAPED_SLASHES));*/

            return json($data);
            if(Helper::$ajax['final_from_wxid'] == $jane || 1){
                Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));


                /*return json($data);*/
            }
        }else{
            exit('非法请求');
        }
    }

    /**
     * wechat-xp机器人消息接收
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function botXpCallback(){
        if(request()->isPost()) {
            Helper::$ajax = $this->getAjax();
            //Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
            $this->botClient = $this->getBotClient('xp', ['base_uri' => '124.223.70.93:8889']);
            $from_group = "23913604451@chatroom";
            $dest_group = "20668619112@chatroom";
            //$dest_group = "21106206097@chatroom";
            $from_user = "wxid_xokb2ezu1p6t21";
            $dest_user = "wxid_a98qqf9m4bny22";
            $wxid = Helper::$ajax['wxid'];
            if(in_array(Helper::$ajax['type'], [Xp::MSG_IMG])){
                $wxid = Helper::$ajax['content']['id1'];
            }
            if($wxid == $from_group && time() > strtotime(date("Ymd 08:00:00")) && time() < strtotime(date("Ymd 23:30:00"))){
                switch (Helper::$ajax['type']){
                    case Xp::MSG_IMG:
                        sleep(2);
                        $this->botClient->sendImgToFriend([
                            'data' => [
                                "to" => $dest_group, "content" => "C:\\Users\\Administrator\\Documents\\WeChat Files\\".Helper::$ajax['content']['thumb']
                            ]
                        ]);
                        break;
                    case Xp::MSG_TEXT:
                        $jtt = new Jtt(['appid' => '2201091352479003', 'appkey' => '6653c1cad8e86ec4050b76cb58fc3cca']);
                        $count = 0;
                        do{
                            $res = $jtt->universal([
                                'unionid' => '2022372433',
                                'positionid' => '3004063211',
                                'content' => Helper::$ajax['content']
                            ]);
                            $count++;
                            if($count >= 2) break;
                        }while($res === false);

                        if($res !== false){
                            $this->botClient->sendTextToFriend([
                                'data' => [
                                    "to" => $dest_group, "content" => $res['chain_content']
                                ]
                            ]);
                        }else{
                            $this->botClient->sendTextToFriend([
                                'data' => [
                                    "to" => $from_user, "content" => '消息:' . Helper::$ajax['content']."发送失败"
                                ]
                            ]);
                        }
                        break;
                }
            }elseif ($wxid == $from_user && Helper::$ajax['content']=='ping'){
                $this->botClient->sendTextToFriend([
                    'data' => [
                        "to" => $from_user, "content" => 'pong！'
                    ]
                ]);
            }

        }else{
            exit("非法请求");
        }
    }

    /**
     * back up
     * wechat-xp机器人消息接收
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function botXpCallback1(){
        if(request()->isPost()) {
            Helper::$ajax = $this->getAjax();
            $this->bot = $this->getBot();
            $this->botClient = $this->getBotClient('xp', ['base_uri' => $this->bot['url']]);
            $from_group = "23913604451@chatroom";
            $dest_group = "20668619112@chatroom";
            //$dest_group = "21106206097@chatroom";

            $dest_user = "wxid_a98qqf9m4bny22";

            switch (Helper::$ajax['type']) {
                case Xp::MSG_TEXT:
                    $this->textHandle();
                    break;
                case Xp::EVENT_USER_LIST:
                    $this->userListHandle();
                    break;
                case Xp::EVENT_PERSONAL_INFO:
                    $this->userInfoHandle();
                    break;
                case Xp::MSG_IMG:
                    $this->imgHandle();
                    break;
            }
        }else{
            exit("非法请求");
        }
    }

    private function textHandle(){
        $wxid = Helper::$ajax['wxid'];
        $from_user = "wxid_xokb2ezu1p6t21";
        //Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
        if($wxid == $from_user){
            //Logger::error(json_encode($this->bot, JSON_UNESCAPED_UNICODE));
            $this->botClient->sendTextToFriend([
                'data' => [
                    "to" => $from_user, "content" => "test"
                ]
            ]);
        }
    }

    /**
     * 获取用户信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function userInfoHandle(){
        $info = Helper::$ajax['content'];
        if(!empty($info['wx_id'])){
            $this->botM->updateOne([
                'id' => $this->bot['id'],
                'uin' => $info['wx_id'],
                'uuid' => $info['wx_code'],
                'nickname' => $info['wx_name']
            ]);
        }
        Logger::error(json_encode($info, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 拉取好友列表处理器
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function userListHandle(){
        foreach (Helper::$ajax['content'] as $v){
            $nickname = filter_emoji($v['name']);
            $remark_name = filter_emoji($v['remarks']);
            $nickname_arr[] = $nickname;
            $type = \app\constants\Bot::FRIEND;
            if(strpos($v['wxid'], '@') !== false){
                $type = \app\constants\Bot::GROUP;
            }elseif (strpos($v['wxid'], 'gh_') !== false){
                $type = \app\constants\Bot::MP;
            }
            if($data = $this->memberM->getOneByMap(['wxid' => $v['wxid']], ['id'])){
                $this->memberM->updateOne([
                    'id' => $data['id'],
                    'nickname' => $nickname,
                    'remark_name' => $remark_name,
                    'username' => $v['wxcode'],
                    'headimgurl' => $v['headimg']
                ]);
            }else{
                $this->memberM->addOne([
                    'bot_id' => $this->bot['id'],
                    'nickname' => $nickname,
                    'remark_name' => $remark_name,
                    'username' => $v['wxcode'],
                    'headimgurl' => $v['headimg'],
                    'wxid' => $v['wxid'],
                    'type' => $type
                ]);
            }
        }
    }

    private function imgHandle(){
        $wxid = Helper::$ajax['content']['id1'];
    }

    /**
     * 机器人消息回调
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function botCallback(){
        if(request()->isPost()){
            Helper::$ajax = $this->getAjax();
            Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
            $this->bot = $this->getBot();
            $this->botClient = $this->getBotClient();
            $this->sender = $this->memberM->getOneByMap(['username' => Helper::$ajax['from']], ['username', 'nickname', 'remark_name', 'type']);
            if(!empty($this->sender)){
                switch ($this->sender['type']){
                    case \app\constants\Bot::GROUP:
                        $this->testEmoji();
                        break;
                    default:
                        $this->testFriend();
                        break;
                }
            }
        }
    }

    private function testFriend(){
        if($this->sender['nickname'] == '傅道集'){
            switch (Helper::$ajax['content']){
                case '群发视频':
                    $res = $this->sendVideoBatch();
                    break;
                case '群发文件':
                    $res = $this->sendFileBatch();
                    break;
                case '群发图片':
                    $res = $this->sendImgBatch();
                    break;
                case '群发文本':
                    $res = $this->sendTextBatch();
                    break;
                case '邀请入群':
                    $res = $this->addFriendsIntoGroup();
                    break;
                case '邀请入多群':
                    $res = $this->addFriendIntoGroups();
                    break;
                case "test":
                    $res = $this->sendTextToFriend();
                    break;
                default:
                    $res = $this->testQyk();
                    break;
            }
            if(empty($res['code'])) {
                Logger::error("消息回复错误：" . $res['msg']);
            }
        }
    }

    private function sendVideoBatch(){
        return $this->botClient->sendVideoBatch([
            'uuid' => $this->bot['uuid'],
            'data' => [
                //'groups' => ['@@194328651289274a2af54e56b5d199b6b36dcdd6b9577d694ddb59f31d0986f3'],
                'friends' => ['@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc'],
                'content' => "https://zyx.images.huihuiba.net/0-5b850b0d5c9a6.mp4"
            ]
        ]);
    }

    private function sendFileBatch(){
        return $this->botClient->sendFileBatch([
            'uuid' => $this->bot['uuid'],
            'data' => [
                //'groups' => ['@@194328651289274a2af54e56b5d199b6b36dcdd6b9577d694ddb59f31d0986f3'],
                'friends' => ['@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc'],
                'content' => "https://zyx.images.huihuiba.net/0-5b4de169d053e.mp3",
                'filename' => 'test.mp3'
            ]
        ]);
    }

    private function sendImgBatch(){
        return $this->botClient->sendImgBatch([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'groups' => ['@@194328651289274a2af54e56b5d199b6b36dcdd6b9577d694ddb59f31d0986f3'],
                'friends' => ['@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc'],
                'content' => "https://zyx.images.huihuiba.net//qtk/subjects/sf.png"
            ]
        ]);
    }

    private function sendTextBatch(){
        return $this->botClient->sendTextBatch([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'groups' => ['@@42b7c4fc860fbb8dd24359b03c1cb6104db404d3bbe6aa9ccf4fd49c629f4f3f'],
                'friends' => ['@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc'],
                'content' => "大家好！"
            ]
        ]);
    }

    private function sendTextToFriend(){
        $qyk = new Qyk();
        $res = $qyk->smart([
            "content" => Helper::$ajax['content'],
        ]);
        $content = Helper::$ajax['content'];
        if(!empty($res['content'])){
            $content = $res['content'];
        }
        return $this->botClient->sendTextToFriend([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'to' => Helper::$ajax['from'],
                "content" => $content,
                "type" => \app\constants\Bot::MSGTYPE_TEXT
            ]
        ]);
    }

    private function testQyk(){
        $qyk = new Qyk();
        $res = $qyk->smart([
            "content" => str_replace(["小乔同学", ",", "，"], "", Helper::$ajax['content']),
        ]);

        if(!empty($res['content'])){
            $content = $res['content'];
        }else{
            $content = "不知道你说神马！";
        }
        return $this->botClient->sendTextToFriend([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'to' => Helper::$ajax['from'],
                "content" => $content,
                "type" => \app\constants\Bot::MSGTYPE_TEXT
            ]
        ]);
    }

    private function testQykGroup(){
        $qyk = new Qyk();
        $res = $qyk->smart([
            "content" => str_replace(["小乔同学", ",", "，"], "", Helper::$ajax['content']),
        ]);

        if(!empty($res['content'])){
            $content = $res['content'];
        }else{
            $content = "不知道你说神马！";
        }
        return $this->botClient->sendTextToGroup([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'to' => Helper::$ajax['from'],
                "content" => $content,
                "type" => \app\constants\Bot::MSGTYPE_TEXT
            ]
        ]);
    }

    private function testEmoji(){
        if(!empty(Helper::$ajax['useringroup'])){
            if(strstr(Helper::$ajax['content'], "小乔同学")){
                $res = $this->testQykGroup();
            }else if(strstr(Helper::$ajax['useringroup'], "蒙圈的海马体") || strstr(Helper::$ajax['useringroup'], "小余")){
                $nickname = htmlspecialchars_decode(explode('@', Helper::$ajax['useringroup'])[0]);
                switch (Helper::$ajax['type']){
                    case \app\constants\Bot::MSGTYPE_IMAGE:
                        $res = $this->botClient->sendTextToGroup([
                            'uuid' => $this->bot['uuid'],
                            'data' => [
                                'to' => Helper::$ajax['from'],
                                "content" => "@{$nickname} 好好说话别发图",
                                "type" => \app\constants\Bot::MSGTYPE_TEXT
                            ]
                        ]);
                        break;
                    default:
                        if(strstr(Helper::$ajax['content'], "闭嘴")){
                            $res = $this->botClient->sendTextToGroup([
                                'uuid' => $this->bot['uuid'],
                                'data' => [
                                    'to' => Helper::$ajax['from'],
                                    "content" => "@{$nickname} 你说脏话",
                                    "type" => \app\constants\Bot::MSGTYPE_TEXT
                                ]
                            ]);
                        }
                        break;
                }
            }else if(strstr(Helper::$ajax['useringroup'], "傅道集")){
                if(Helper::$ajax['content'] == '移除好友'){
                    $res = $this->botClient->removeMembersFromGroup([
                        'uuid' => $this->bot['uuid'],
                        'data' => [
                            'group' => Helper::$ajax['from'],
                            "members" => ["@284e2092658cc77112e6042b3b8e529f35b51df3753269dc170f839ccf535045"/*, "@1fa45f6038a8c73490d52d93d0e88c0b"*/]
                        ]
                    ]);
                }
            }
            if(!empty($res) && $res['code'] == 0) {
                Logger::error("消息回复错误：" . $res['msg']);
            }
        }
    }

    private function addFriendsIntoGroup(){
        return $this->botClient->addFriendsIntoGroup([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'group' => '@@42b7c4fc860fbb8dd24359b03c1cb6104db404d3bbe6aa9ccf4fd49c629f4f3f',
                'friends' => ['@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc']
            ]
        ]);
    }

    private function addFriendIntoGroups(){
        return $this->botClient->addFriendIntoGroups([
            'uuid' => $this->bot['uuid'],
            'data' => [
                'groups' => [
                    '@@42b7c4fc860fbb8dd24359b03c1cb6104db404d3bbe6aa9ccf4fd49c629f4f3f',
                    '@@194328651289274a2af54e56b5d199b6b36dcdd6b9577d694ddb59f31d0986f3'
                ],
                'friend' => '@d8d567bae469380473a9f7763c8bec6375bf7044d4b0018f86125fb514d387bc'
            ]
        ]);
    }

    /**
     * @param string $driver
     * @param array $options
     * @return Wx|Xp|Kam|Vlw
     */
    private function getBotClient($driver = 'web', $options = []){
        switch ($driver){
            case \app\constants\Bot::PROTOCOL_KAM:
                $client = new Kam($options);
                break;
            case \app\constants\Bot::PROTOCOL_VLW:
                $client = new Vlw($options);
                break;
            case \app\constants\Bot::PROTOCOL_WEB:
                $options = array_merge($options, ['app_key' => Helper::$ajax['appkey']]);
                $client = new Wx($options);
                break;
            default:
                $options = array_merge($options, ['app_key' => Helper::$ajax['appkey']]);
                $client = new Xp($options);
                break;

        }

        return $client;
    }

    /**
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function getBot(){
        return $this->botM->getOneByMap(['app_key' => Helper::$ajax['appkey']]);
    }

    /**
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    private function getMember(){
        return $this->memberM->getOneByMap(['username' => Helper::$ajax['from']]);
    }
}